<script setup>
import { ref, onMounted } from 'vue'
import * as AddressAPI from '@/apis/address'

const addressLsit = ref([])
const getOwnAddress = async () => {
    const res = await AddressAPI.getOwnAddressAPI()
    if (res.code === 2000) {
        addressLsit.value = res.data
    }
}

const delAddress = async (id) => {
    // console.log('删除...', id);
    const res = await AddressAPI.delAddressAPI(id)
    if (res.code === 2000) {
        getOwnAddress()
        ElMessage({ type: 'success', message: '删除成功' })
    }
}

const setDefault = async (id) => {
    // console.log('默认...', id);
    const res = await AddressAPI.setDefaultAPI(id)
    if (res.code === 2000) {
        getOwnAddress()
    }
}

const emptyObj = {
    consignee: '',
    detail: '',
    phone: '',
    isDefault: false
}

const addressObj = ref({ ...emptyObj })

let dialogTitle = ref('')
let dialogVisible = ref(false)
let dialogType = ref('1')
const changeDialogVisible = (type, address) => {
    dialogType.value = type
    if (type === 1) {
        dialogTitle.value = '添加地址'
        addressObj.value = { ...emptyObj }
    }
    else if (type === 2) {
        dialogTitle.value = '编辑地址'
        addressObj.value = { ...address } 
    }
    dialogVisible.value = true
}

const check = () => {
    if (addressObj.value.consignee === '' || addressObj.value.detail === '' || addressObj.value.phone === '') {
        ElMessage({ type: 'warning', message: '请填写完整的地址信息' })
        return false
    }
    return true
}

const confirm = async () => {
    if (check()) {
        if (dialogType.value === 1) {
            // console.log("添加...")
            // console.log(addressObj.value);
            const res = await AddressAPI.addAddressAPI({ ...addressObj.value })
            if (res.code === 2000) {
                getOwnAddress()
                ElMessage({ type: 'success', message: '添加成功' })
            }
        }
        else if (dialogType.value === 2) {
            // console.log("编辑...")
            // console.log(addressObj.value);
            const res = await AddressAPI.updateAddressAPI({
                addrId: addressObj.value.id,
                consignee: addressObj.value.consignee,
                detail: addressObj.value.detail,
                phone: addressObj.value.phone,
                isDefault: addressObj.value.isDefault
            })
            if (res.code === 2000) {
                getOwnAddress()
                ElMessage({ type: 'success', message: '修改成功' })
            }
        }
        dialogVisible.value = false
    }
}

onMounted(() => {
    getOwnAddress()
})
</script>

<template>
<div class="address-page">
    <div class="page-title">我的收货地址</div>
    <a href="javascript:;" class="add-btn" @click="changeDialogVisible(1)">添加地址</a>
    <div class="address-list">
        <div class="address-card" v-for="i in addressLsit" :key="i.id">
            <div class="address-info">
                <div class="info-item">
                    <div class="label">收货人：</div>
                    <div>{{ i.consignee }}</div>
                </div>
                <div class="info-item">
                    <div class="label">收货地址：</div>
                    <div>{{ i.detail }}</div>
                </div>
                <div class="info-item">
                    <div class="label">收货手机号：</div>
                    <div>{{ i.phone }}</div>
                </div>
            </div>
            <div class="address-operation">
                <a href="javascript:;" @click="setDefault(i.id)" v-show="!i.isDefault">设为默认地址</a>
                <a href="javascript:;" @click="changeDialogVisible(2, i)">编辑</a>
                <el-popconfirm title="确认删除吗?" confirm-button-text="确认" cancel-button-text="取消" @confirm="delAddress(i.id)">
                    <template #reference>
                        <a href="javascript:;">删除</a>
                    </template>
                </el-popconfirm>
            </div>
            <div class="default-tag" v-show="i.isDefault">默认收货地址</div>
        </div>
    </div>
</div>

<el-dialog
    v-model="dialogVisible"
    :title="dialogTitle"
    width="30%">
    <div class="dialog-content">
        <div>
            <div class="content-label">收货人：</div>
            <div class="content-ipt"><input type="text" v-model="addressObj.consignee"></div>
        </div>
        <div>
            <div class="content-label">收货地址：</div>
            <div class="content-ipt"><input type="text" v-model="addressObj.detail"></div>
        </div>
        <div>
            <div class="content-label">收货手机号：</div>
            <div class="content-ipt"><input type="text" v-model="addressObj.phone"></div>
        </div>
        <div>
            <div class="content-label"></div>
            <div class="content-ipt switch">
                <el-switch v-model="addressObj.isDefault"/>
                <div style="margin-left: 10px;">设为默认地址</div>
            </div>
        </div>
    </div>
    <template #footer>
        <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="confirm">确定</el-button>
        </span>
    </template>
</el-dialog>
</template>

<style scoped lang='scss'>
.address-page {
    width: 100%;
    height: 100%;
    padding: 20px;
    background-color: #fff;
}

.page-title {
    font-size: 21px;
    padding: 15px 0;
    border-bottom: 1px solid #e8e8e8;
}

.add-btn {
    display: block;
    width: 90px;
    height: 35px;
    line-height: 35px;
    border-radius: 4px;
    font-size: 14px;
    color: #fff;
    background-color: #39bf3e;
    text-align: center;
    margin: 20px 0;
}

.address-card {
    position: relative;
    display: flex;
    justify-content: space-between;
    width: 100%;
    padding: 15px;
    border-radius: 4px;
    border: 1px dashed #a6a6a6;
    color: #525252;
    font-size: 14px;
    margin-bottom: 20px;

    .default-tag {
        position: absolute;
        top: 0;
        right: 0;
        padding: 5px 10px;
        border-top-right-radius: 4px;
        font-size: 14px;
        color: #39bf3e;
        background-color: #f0f9eb;
    }

    .info-item {
        display: flex;
        margin-bottom: 15px;

        &:last-child {
            margin-bottom: 0;
        }
    }

    .address-operation {
        display: flex;
        align-items: center;

        a {
            display: inline-block;
            font-size: 14px;
            color: #39bf3e;
            margin: 0 5px;
        }
    }
}

.dialog-content {
    width: 100%;

    >div {
        display: flex;
        align-items: center;
        margin-bottom: 20px;

        &:last-child {
            margin-bottom: 0;
        }
    }

    .content-label {
        width: 90px;
        color: #666;
        font-weight: bold;
    }

    .content-ipt {
        width: 300px;
        height: 35px;
        border: 1.5px solid #d8d8d8;
        border-radius: 5px;

        input {
            width: 100%;
            height: 100%;
            font-size: 16px;
            padding: 0 10px;
            border-radius: 5px;
        }
    }

    .switch {
        display: flex;
        justify-content: end;
        align-items: center;
        border: none;
        border-radius: 0;
    }
}

.dialog-footer button:first-child {
    margin-right: 10px;
}
</style>
